package com.bupt.liutong.admin.shop.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bupt.liutong.admin.core.base.BaseAction;
import com.bupt.liutong.admin.shop.bo.ProdBo;
import com.bupt.liutong.admin.shop.bo.ProdTypeBo;
import com.bupt.liutong.admin.shop.form.ProdForm;
import com.bupt.liutong.util.BeanUtils;

public class ProdAction extends BaseAction {

	private ProdBo prodBo;
	private ProdTypeBo prodTypeBo;
	
	public ActionForward getProdsByPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ProdForm prodForm = (ProdForm) form;
		request.setAttribute("prodFormList", prodBo.getProdsByPage(prodForm, request));
		putPageInfo(request, prodForm);
		return mapping.findForward("getProdsByPageSuccess");
	}
	public ActionForward insertProd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("prodTypeFormList", prodTypeBo.getAllProdTypes());
		return mapping.findForward("insertProdSuccess");
	}
	
	public ActionForward insertProdDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ProdForm prodForm = (ProdForm) form;
		prodBo.insertProd(prodForm, request);
		return mapping.findForward("insertProdDoSuccess");
	}
	
	public ActionForward updateProd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ProdForm prodForm = (ProdForm) form;
		request.setAttribute("prodTypeFormList", prodTypeBo.getAllProdTypes());
		BeanUtils.copyProperties(prodForm, prodBo.getProdById(prodForm));
		return mapping.findForward("updateProdSuccess");
	}
	
	public ActionForward updateProdDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ProdForm prodForm = (ProdForm) form;
		prodBo.updateProd(prodForm, request);
		return mapping.findForward("updateProdDoSuccess");
	}
	
	public ActionForward deleteProds(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("id");
		prodBo.deleteProds(ids);
		return mapping.findForward("deleteProdsSuccess");
	}

	public void setProdBo(ProdBo prodBo) {
		this.prodBo = prodBo;
	}
	
	public void setProdTypeBo(ProdTypeBo prodTypeBo) {
		this.prodTypeBo = prodTypeBo;
	}
}
